<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * @param string $url
 * @param array $content
 * @param string $file
 * @return bool|string
 */
function http_request(string $url, $content = [], $file = '') {
    if (!empty($file)) {
        // 文件上传
        //TODO:: php5.5之前， '@'.$file; 就可以文件上传
        //$content['file'] = '@'.$file;
        // PHP5.6之后
        $content['file'] = new CURLFile($file);
    }
    //1.初始化
    $ch = curl_init();
    //2.相关设置
    curl_setopt($ch, CURLOPT_URL, $url);//url
    curl_setopt($ch, CURLOPT_HEADER, 0);//请求头关闭
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接返回，以字符串方式返回 【必写】
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);//超时时间
    curl_setopt($ch, CURLOPT_USERAGENT, 'MSIE');//浏览器型号
    //TODO:: 组合使用
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);//ssl证书不检测 域名
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);//ssl证书不检测

    // 如果content不为假，则为POST请求
    if ($content) {
        // POST请求
        curl_setopt($ch, CURLOPT_POST, 1);
        // POST请求数据
        curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
    }
    //3.发起请求
    $data = curl_exec($ch);
    // 出错
    if (curl_errno($ch) > 0) {
        $data = curl_error($ch);
    }
    //4.关闭请求
    curl_close($ch);
    // 输出
    return $data;
}